<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2018/12/3
 * Time: 14:24
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Counts;
use App\Models\CountsLog;

class CountLogController extends Controller
{

    /**
     * 获取数据统计记录列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(){

        $params = [
            'start' => (string)request('start',''),
            'end' => (string)request('end',''),
        ];
        $lists  = CountsLog::where(function($query) use($params){
                if ($params['start']){
                    $query->whereBetween('date',[$params['start'],$params['end']]);
                }
            })
            ->orderBy('id','DESC')
            ->get();

        $newArr = array();
        foreach ($lists as $key => $item) {
            $newArr[$item['date']]['_cids'][] = $item['cid'];
            $newArr[$item['date']]['date'] = $item['date'];
            $newArr[$item['date']]['cids'][] = [
                'cid' => $item['cid'],
                'total' => $item['total']
            ];
        }

        $newArrs = [];
        foreach ($newArr as  $key=>$item){
            $newArrs[] = $item;
        }
        return $this->returnJson(0,'',$newArrs);
    }
}